﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;

namespace Hongsh.Pay.Client.Proxy.Http {
    // Token: 0x02000038 RID: 56
    public class HttpServer : IDisposable {
        // Token: 0x170000A0 RID: 160
        // (get) Token: 0x06000186 RID: 390 RVA: 0x0000312F File Offset: 0x0000132F
        public string StaticFilePath { get; }

        // Token: 0x170000A1 RID: 161
        // (get) Token: 0x06000187 RID: 391 RVA: 0x00003137 File Offset: 0x00001337
        // (set) Token: 0x06000188 RID: 392 RVA: 0x0000313F File Offset: 0x0000133F
        public int ReadTimeOut { get; set; }

        // Token: 0x170000A2 RID: 162
        // (get) Token: 0x06000189 RID: 393 RVA: 0x00003148 File Offset: 0x00001348
        // (set) Token: 0x0600018A RID: 394 RVA: 0x00003150 File Offset: 0x00001350
        public int WriteTimeOut { get; set; }

        // Token: 0x0600018B RID: 395 RVA: 0x0000C968 File Offset: 0x0000AB68
        public void Start() {
            foreach (HttpLisenter httpLisenter in LisenterList) {
                httpLisenter.Listen();
            }
        }

        // Token: 0x0600018C RID: 396 RVA: 0x0000C9BC File Offset: 0x0000ABBC
        public void Stop() {
            foreach (HttpLisenter httpLisenter in LisenterList) {
                httpLisenter.Stop();
            }
        }

        // Token: 0x0600018D RID: 397 RVA: 0x00003159 File Offset: 0x00001359
        public void AddLisenter(IPEndPoint ep) {
            LisenterList.Add(new HttpLisenter(this, ep));
        }

        // Token: 0x0600018E RID: 398 RVA: 0x0000CA10 File Offset: 0x0000AC10
        internal void OnHttpConnection(HttpConnection conn) {
            Dictionary<string, HttpConnection> connectionList = ConnectionList;
            lock (connectionList) {
                ConnectionList[conn.Id] = conn;
            }
            Console.WriteLine("接受：" + conn.Id);
            conn.Process();
        }

        // Token: 0x0600018F RID: 399 RVA: 0x0000CA78 File Offset: 0x0000AC78
        internal void OnHttpConnectionClosed(HttpConnection conn) {
            Dictionary<string, HttpConnection> connectionList = ConnectionList;
            lock (connectionList) {
                ConnectionList.Remove(conn.Id);
            }
            Console.WriteLine("关闭：" + conn.Id);
        }

        // Token: 0x06000190 RID: 400 RVA: 0x0000CADC File Offset: 0x0000ACDC
        internal void OnHttpContext(HttpContext ctx) {
            string str = "开始处理：";
            Uri url = ctx.Request.Url;
            Console.WriteLine(str + ((url != null) ? url.ToString() : null));
            foreach (HttpContextActionDelegate httpContextActionDelegate in Handlers) {
                try {
                    string str2 = "调用处理器：";
                    Type declaringType = httpContextActionDelegate.Method.DeclaringType;
                    Console.WriteLine(str2 + ((declaringType != null) ? declaringType.ToString() : null) + "." + httpContextActionDelegate.Method.Name);
                    httpContextActionDelegate(ctx);
                }
                catch (Exception exception) {
                    ctx.Exception = exception;
                }
            }
            string str3 = "结束处理：";
            Uri url2 = ctx.Request.Url;
            Console.WriteLine(str3 + ((url2 != null) ? url2.ToString() : null));
        }

        // Token: 0x06000191 RID: 401 RVA: 0x0000316D File Offset: 0x0000136D
        internal void OnHttpException(Exception e) {
            Console.WriteLine("异常：" + ((e != null) ? e.ToString() : null));
        }

        // Token: 0x06000192 RID: 402 RVA: 0x0000318B File Offset: 0x0000138B
        private static void HttpActionHandler(HttpContext ctx) {
            if (ctx.Action != null) {
                ctx.Action(ctx);
            }
        }

        // Token: 0x06000193 RID: 403 RVA: 0x0000CBCC File Offset: 0x0000ADCC
        private static void HttpResultHandler(HttpContext ctx) {
            if (ctx.Exception != null) {
                ctx.Result = new HttpTextResult(ctx.Exception.Message, "text/plain") {
                    Code = 500
                };
            }
            if (ctx.Result == null) {
                ctx.Result = HttpResult.Ok;
            }
            ctx.Response.Header["Date"] = DateTime.Now.ToString();
            ctx.Result.WriteReponse(ctx);
            ctx.Response.Flush();
        }

        // Token: 0x06000194 RID: 404 RVA: 0x0000CC5C File Offset: 0x0000AE5C
        private static void HttpRouteHandler(HttpContext ctx) {
            string text = ctx.Request.Method + ctx.Request.Url.AbsolutePath;
            foreach (KeyValuePair<string, HttpContextActionDelegate> keyValuePair in ctx.Server.RouterList) {
                if (text.Equals(keyValuePair.Key, StringComparison.OrdinalIgnoreCase)) {
                    ctx.Action = keyValuePair.Value;
                    break;
                }
            }
            if (ctx.Action == null) {
                ctx.Result = HttpResult.NoFound;
            }
        }

        // Token: 0x06000195 RID: 405 RVA: 0x0000CD04 File Offset: 0x0000AF04
        private static void HttpStaticHandler(HttpContext ctx) {
            if (ctx.Action == null && (ctx.Result == null || ctx.Result.Code == 404)) {
                string path = ctx.Request.Url.AbsolutePath.TrimStart(new char[]
                {
                    '/'
                });
                FileInfo fileInfo = new FileInfo(Path.Combine(ctx.Server.StaticFilePath, path));
                if (fileInfo.Exists) {
                    ctx.Result = new HttpFileResult(fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                }
            }
        }

        // Token: 0x06000196 RID: 406 RVA: 0x000031A4 File Offset: 0x000013A4
        public void Dispose() {
            Stop();
        }

        // Token: 0x06000197 RID: 407 RVA: 0x0000CD90 File Offset: 0x0000AF90
        public HttpServer() {
            //Class15.cwcZ3SWztHroi();
            StaticFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "wwwroot");
            ConnectionList = new Dictionary<string, HttpConnection>();
            RouterList = new Dictionary<string, HttpContextActionDelegate>();
            LisenterList = new List<HttpLisenter>();
            Context = new Dictionary<string, object>();
            CertificateList = new Dictionary<string, string>();
            Handlers = new List<HttpContextActionDelegate>
            {
                new HttpContextActionDelegate(HttpRouteHandler),
                new HttpContextActionDelegate(HttpStaticHandler),
                new HttpContextActionDelegate(HttpActionHandler),
                new HttpContextActionDelegate(HttpResultHandler)
            };
            ReadTimeOut = 20000;
            WriteTimeOut = 20000;
            //base..ctor();
        }

        // Token: 0x040000AA RID: 170
        public readonly Dictionary<string, HttpConnection> ConnectionList;

        // Token: 0x040000AB RID: 171
        public readonly Dictionary<string, HttpContextActionDelegate> RouterList;

        // Token: 0x040000AC RID: 172
        public readonly List<HttpLisenter> LisenterList;

        // Token: 0x040000AD RID: 173
        public readonly Dictionary<string, object> Context;

        // Token: 0x040000AE RID: 174
        public readonly Dictionary<string, string> CertificateList;

        // Token: 0x040000AF RID: 175
        public readonly List<HttpContextActionDelegate> Handlers;
    }
}
